package configmap

import (
	"mariadb-operator/pkg/apis/finupgroup/v1alpha1"
	"mariadb-operator/pkg/constants"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// New will return a new kubernetes configmap for mariadb cluster
func New(cluster *v1alpha1.MariaDBCluster) *v1.ConfigMap {
	if cluster.Spec.RestartPolicy == "" {
		cluster.Spec.RestartPolicy = constants.OrderRestartPolicy
	}
	return &v1.ConfigMap{
		TypeMeta: metav1.TypeMeta{
			APIVersion: "v1",
			Kind:       "ConfigMap",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:      constants.DefaultMariaDBConfigMap,
			Namespace: cluster.Namespace,
			Annotations: map[string]string{
				constants.ReferenceMariaDBClusterKey: cluster.Name,
				constants.RestartPolicyKey:           cluster.Spec.RestartPolicy,
			},
		},
		Data: map[string]string{
			"master.cnf": `
			# Apply this config only on the master.
			[mysqld]
			log_bin=log-bin             #启动二进制日志并指定文件名
			skip_name_resolve=on        #跳过主机名解析。在CentOS 6自带的mysql后面的=on不用写
			innodb_file_per_table=on    #innodb的每个表是用单独的文件`,
			"slave.cnf": ` 
			# Apply this config only on slaves.
			[mysqld]
			relay_log=relay-log        
			#启用中继日志。在数据目录下有一个relay-kog.info里面保存了当前的中继日志和位置会主节点二进制文件的名字和位置。
			read_only=on                #禁止用户写入数据，这一项的管理员和复制重放无效。`,
		},
	}
}
